//
//  TPPayViewController.m
//  Tripvi
//
//  Created by xiaowuxiaowu on 2017/5/16.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "TPPayViewController.h"
#import "DatePickerActionSheet.h"
#import "TPPayCell.h"
#import "UITableView+TP.h"
#import "TPPayModel.h"
#import "NSDate+Extension.h"
#import "TPUserBookingViewController.h"
#import "TPUpdateTelViewController.h"
@interface TPPayViewController (){
    DatePickerActionSheet *sheet;
}
@property (nonatomic,assign)BOOL isWechatPay;
@property (nonatomic,strong)NSMutableArray *datas;
@end

@implementation TPPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"预约";
    [self setup];
}

-(void)setup{
    
    self.isWechatPay = YES;
    self.dataSource = [[NSMutableArray alloc]initWithArray:@[xw_getNULLString(@"产品名称："),@"预约时间：",@"预约金额：",@"支付方式："]];
    
    NSDate *date = [[NSDate date]dateByAddingDays:1];

    self.datas = [NSMutableArray arrayWithObjects:xw_getNULLString(self.detailModel.goods_name),[date stringWithFormat:kNSDateHelperFormatSQLDateWithTimeYYYYMMDDHHmmss],[NSString stringWithFormat:@"￥%@",self.detailModel.reservation_fee],@"", nil];
    
    UIView *headerView = [self.tableView tp_addheaderWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 200.0f)];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200.0, 120.0)];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    [imageView ym_setImageWithURL:self.detailModel.goods_image placeholderImage:kTemplate_placeholder];
    [headerView addSubview:imageView];
    imageView.centerY = headerView.height/2;
    imageView.centerX = self.view.width/2;
    
    
    WEAKSELF;
    UIButton *button = [self.tableView tp_addFooterWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 150.0f) buttonHeight:38.0 title:@"确定支付" toBlock:^(NSInteger tag, UIButton *button) {
        
        [weakSelf getPayRequest];
       
    }];
    [button mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.4);
    }];
    [button layoutIfNeeded];
    
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    button.contentMode = UIViewContentModeScaleAspectFit;
    
}

//请求
-(void)getPayRequest{
    
      if(!tp_isLogin())
          return;
    
    if([LoginModel shareLoginModel].mobile.length == 0 || [LoginModel shareLoginModel].user_name.length == 0){
        
        [UIAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
            
            if(buttonIndex == 1){
                 [self.navigationController pushViewController:[TPUpdateTelViewController new] animated:YES];
            }
            
        } title:@"温馨提示" message:@"资料信息还没有完善，是否要去完善" cancelButtonName:@"取消" otherButtonTitles:@"去完善", nil];
       
        return;
    }
//    [[TPRequestClient sharedClient]getSubmitAppointmentShopId:self.detailModel.goods_id appoint_time:self.datas[1] appoint_money:self.detailModel.reservation_fee payment_method:self.isWechatPay ? @"WXPAY":@"ALIPAY" success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
//
//         [self.view xw_hideTitle:@"预约成功!!"];
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [self leftBtnAction:nil];
//
//        });
//
//
//    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
//        [self.view xw_hideTitle:respone.message];
//    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
//        [self.view xw_hideTitle:error.localizedDescription];
//    }];
//
//
    
  [TPPayModel addSubmitAppointmentShopId:self.detailModel.goods_id appoint_time:self.datas[1] appoint_money:self.detailModel.reservation_fee isWechatPay:self.isWechatPay block:^(BOOL isSccuess, NSString *message, NSDictionary *dict) {
   
      [UIAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
          
          if(isSccuess){
              
              [self.view xw_hideTitle:@"预约成功!!"];
              dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                  TPUserBookingViewController *bookingVC = [TPUserBookingViewController new];
                  bookingVC.type = TPAppointmentTypeBooking;
                  [self.navigationController pushViewController:bookingVC animated:YES];
              });
          }
          
      } title:@"微信提示" message:isSccuess?@"预约成功":message cancelButtonName:@"确定" otherButtonTitles:nil, nil];
      
      

  }];
}


#pragma mark---tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TPPayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellx"];
    if(!cell){
        cell = [[TPPayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellx"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
    }
    cell.accessoryType = indexPath.row == 1 ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    
    cell.indexPath = indexPath;
    cell.title_label.text = self.dataSource[indexPath.row];
    cell.detail_label.text = self.datas[indexPath.row];
    cell.isWechatPay = self.isWechatPay;
    @weakify(self);
    [cell setActionBlock:^(BOOL isWechat) {
        @strongify(self);
        self.isWechatPay = isWechat;
    }];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0)
        return 44.0f;
    return 55.0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.row == 1){
        
       sheet = [[DatePickerActionSheet alloc]initWithDate:self.datas[indexPath.row] datePickerMode:UIDatePickerModeDateAndTime];
        sheet.datePicker.minimumDate = [NSDate date];
        [sheet showBlock:^(NSString *dateString, NSDate *date) {
            [self.datas replaceObjectAtIndex:1 withObject:dateString];
            [tableView reloadData];
        }];
    }
//    if(indexPath.row == 2)
//        self.isWechatPay = YES;
//    else if (indexPath.row == 3)
//        self.isWechatPay = NO;
    
    [tableView reloadData];
}


@end
